<template>
  <div class="activity-type-manage">
    <el-card>
      <div class="header-bar">
        <span>活动类型列表</span>
        <el-button type="primary" size="small" @click="openAddType">新增类型</el-button>
      </div>
      <el-table :data="typeList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="name" label="类型名称" />
        <el-table-column prop="sort" label="排序" width="80" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" @click="openEditType(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDeleteType(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑类型弹窗 -->
    <el-dialog v-model="typeDialogVisible" :title="typeForm.id ? '编辑类型' : '新增类型'">
      <el-form :model="typeForm" label-width="80px">
        <el-form-item label="类型名称">
          <el-input v-model="typeForm.name" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="typeForm.sort" :min="0" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="typeForm.status" :active-value="1" :inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="typeDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSaveType">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getActivityTypeList, addActivityType, updateActivityType, deleteActivityType } from '@/api/admin/activityType'

const typeList = ref([])
const typeDialogVisible = ref(false)
const typeForm = ref({ id: null, name: '', sort: 0, status: 1 })

const fetchTypes = async () => {
  const res = await getActivityTypeList()
  if (res.code === 200) {
    typeList.value = res.data
  }
}

const openAddType = () => {
  typeForm.value = { id: null, name: '', sort: 0, status: 1 }
  typeDialogVisible.value = true
}

const openEditType = (row) => {
  typeForm.value = { ...row }
  typeDialogVisible.value = true
}

const handleSaveType = async () => {
  if (!typeForm.value.name) {
    ElMessage.warning('类型名称不能为空')
    return
  }
  let res
  if (typeForm.value.id) {
    res = await updateActivityType(typeForm.value)
  } else {
    res = await addActivityType(typeForm.value)
  }
  if (res.code === 200) {
    ElMessage.success('保存成功')
    typeDialogVisible.value = false
    fetchTypes()
  } else {
    ElMessage.error(res.msg || '保存失败')
  }
}

const handleDeleteType = (row) => {
  ElMessageBox.confirm('确定要删除该类型吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const res = await deleteActivityType(row.id)
    if (res.code === 200) {
      ElMessage.success('删除成功')
      fetchTypes()
    } else {
      ElMessage.error(res.msg || '删除失败')
    }
  }).catch(() => {})
}

onMounted(() => {
  fetchTypes()
})
</script>

<style scoped>
.activity-type-manage {
  padding: 20px;
}
.header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
</style> 